﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;


namespace MemoGameService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://memogameservice.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        //String gamePath = @"D:\IFE\sem II\virtual environment\project\MemoGame\MemoGame\bin\Debug\";
        //String xmlFilesPath = @"D:\IFE\sem II\virtual environment\project\MemoGame\MemoGame\Resources\";
        String gamePath = @"D:\Projekty\2_VE\MemoGameForKinect\MemoGame\MemoGame\bin\Debug\";
        String xmlFilesPath = @"D:\Projekty\2_VE\MemoGameForKinect\MemoGame\MemoGame\Resources\";

        [WebMethod]
        public int CalculateSumTestMethod(int a, int b)
        {
            return a + b;
        }

        [WebMethod]
        public String GetCredits()
        {
            return "Agata Tarnowska \n Sebastian Pasiecznik \n Zbyszko Natkański";
            
        }

        [WebMethod]
        public XmlDocument GetHallOfFame()
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(xmlFilesPath + "hall_of_fame.xml");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            return doc;
        }

        [WebMethod]
        public XmlDocument GetOptions()
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(xmlFilesPath + "game_options.xml");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
            return doc;
        }

        [WebMethod]
        public Boolean SetOptions(int boardSize, String playerName)
        {
            try
            {
                String str = "<?xml version=\"1.0\"?><Options xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><person_name>" +
                playerName + "</person_name><board_size>" + boardSize +
                "</board_size></Options>";

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(str);
                xmlDoc.Save(xmlFilesPath + "game_options.xml");
            }
            catch (Exception e)
            {
                e.ToString();
                return false;
            }
            return true;
            
        }

        [WebMethod]
        public Boolean RunKinectMemoGame(String command)
        {
            ProcessStartInfo proc = new ProcessStartInfo();
            proc.CreateNoWindow = false;
            proc.UseShellExecute = true;
            proc.WorkingDirectory = gamePath;
            proc.FileName = "MemoGame.exe";
            if (!command.Equals(""))
            {
                proc.Arguments = command;
            }            
            Process.Start(proc);

            return true;
        }
    }
}